package com.fsc.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener{
	
	//油上角button按钮
	private Button btn_system;
	private int position;
	// 底部菜单4个Linearlayout
	private LinearLayout ll_home;
	private LinearLayout ll_income;
	private LinearLayout ll_outcome;
	private LinearLayout ll_flag;
	
	//FrameLayout 中加载的四个布局
	private Fragment l1,l2,l3,l4;
	
	//Fragment管理器
	private FragmentManager fm;
	
	//底部菜单4个TextView
	private TextView tv_home;
	private TextView tv_income;
	private TextView tv_outcome;
	private TextView tv_flag;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		initView();
		initEvent();
		select(0);
		
		btn_system.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent objIntent = new Intent(MainActivity.this,AddPasswordActivity.class);
				startActivity(objIntent);
			}
		});
	}
	
	private void initEvent() {
		// 设置按钮监听
		ll_home.setOnClickListener(this);
		ll_income.setOnClickListener(this);
		ll_outcome.setOnClickListener(this);
		ll_flag.setOnClickListener(this);
		btn_system.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ll_home:
			//1、加载当前的fragment
			tv_home.setTextColor(0xff1B940A);
			tv_income.setTextColor(0xffffffff);
			tv_outcome.setTextColor(0xffffffff);
			tv_flag.setTextColor(0xffffffff);
			select(0);
			break;
		case R.id.ll_income:
			tv_home.setTextColor(0xffffffff);
			tv_income.setTextColor(0xff1B940A);
			tv_outcome.setTextColor(0xffffffff);
			tv_flag.setTextColor(0xffffffff);
			select(1);
			break;
		case R.id.ll_outcome:
			tv_home.setTextColor(0xffffffff);
			tv_income.setTextColor(0xffffffff);
			tv_outcome.setTextColor(0xff1B940A);
			tv_flag.setTextColor(0xffffffff);
			select(2);
			break;
		case R.id.ll_flag:
			tv_home.setTextColor(0xffffffff);
			tv_income.setTextColor(0xffffffff);
			tv_outcome.setTextColor(0xffffffff);
			tv_flag.setTextColor(0xff1B940A);
			select(3);
			break;
		
		default:
			break;
		}
	}
	
	private void select(int i) {
		fm = getFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();
		hiden(ft);
		switch (i) {
			case 0:
				if(l1 ==null){
					l1 = new HomeActivity();
					ft.add(R.id.fl_content, l1);
				}else{
					ft.show(l1);
				}
				break;
				
			case 1:
				if(l2 ==null){
					l2 = new IncomeActivity();
					ft.add(R.id.fl_content, l2);
				}else{
					ft.show(l2);
				}
				break;
				
			case 2:
				if(l3 ==null){
					l3 = new OutcomeActivity();
					ft.add(R.id.fl_content, l3);
				}else{
					ft.show(l3);
				}
				break;
				
			case 3:
				if(l4 ==null){
					l4 = new FlagActivity();
					ft.add(R.id.fl_content, l4);
				}else{
					ft.show(l4);
				}
				break;
			default:
				break;
		}
		//提交
		ft.commit();
	}

	private void hiden(FragmentTransaction ft) {
		if(l1 != null){
			ft.hide(l1);
		}
		if(l2 != null){
			ft.hide(l2);
		}
		if(l3 != null){
			ft.hide(l3);
		}
		if(l4 != null){
			ft.hide(l4);
		}
	}

	private void initView() {
		// 底部菜单4个Linearlayout
		this.ll_home = (LinearLayout) findViewById(R.id.ll_home);
		this.ll_income = (LinearLayout) findViewById(R.id.ll_income);
		this.ll_outcome = (LinearLayout) findViewById(R.id.ll_outcome);
		this.ll_flag = (LinearLayout) findViewById(R.id.ll_flag);
		
		//按钮button初始化
		this.btn_system = (Button) findViewById(R.id.btn_system);
		
        // 底部菜单4个菜单标题
        this.tv_home = (TextView) findViewById(R.id.tv_home);
        this.tv_income = (TextView) findViewById(R.id.tv_income);
        this.tv_outcome = (TextView) findViewById(R.id.tv_outcome);
        this.tv_flag = (TextView) findViewById(R.id.tv_flag); 
	}
}
